package com.lby.redisdemo.controller;


import cn.hutool.core.util.IdUtil;
import com.lby.redisdemo.bean.OrderBean;
import com.lby.redisdemo.bean.SimpleBean;
import com.lby.redisdemo.config.SimpleProducer;
import com.lby.redisdemo.constant.SimpleMqConstant;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName SendMessageController
 * @Description TODO
 * @Author louby0923
 * @Date 2023/5/30 11:33
 * @Version 1.0
 */
@Slf4j
@RestController
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
    /*@Autowired
    private SimpleProducer simpleProducer;*/

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    @GetMapping("/sendmq")
    public void testSimple() throws Exception {
        for (int i = 0; i < 10; i++) {
            SimpleBean simpleBean = SimpleBean.builder().createTime(new Date()).name("JulyWhj").age(i).no("ID-0001").phone("138XXXXXXXX").build();
            // simpleProducer.sendOrderMessage(simpleBean);

            System.out.println("生成次数" + i);
        }
    }

    @GetMapping("/sendmq2")
    public String contextLoads() {
        for (long i = 1; i < 2; i++) {
            //交换机名称
            String exchangeName = "direct-order-exchange";
            //路由key
            String routingKey = "sms";
            OrderBean order = buildOrder(i);
            // orderService.createOrder(order, routingKey, exchangeName);


            // 开启生产者回调
            rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
                @Override
                public void confirm(CorrelationData correlationData, boolean b, String s) {
                    if (b) {
                        log.info("消息发送到交换机成功");
                    } else {
                        log.error("消息发送到交换机失败,失败信息[{}]", s);
                    }
                }
            });
            rabbitTemplate.convertAndSend(SimpleMqConstant.EXCHANGE_NAME, routingKey, order);
        }
        return "发送成功！";
    }

    private OrderBean buildOrder(long id) {
        OrderBean order = new OrderBean();
        order.setRequestId(String.valueOf(id));
        order.setUserId(String.valueOf(id));
        order.setOrderNo(IdUtil.randomUUID());
        order.setAmount(new BigDecimal(10));
        order.setGoodsNum(1);
        order.setTotalAmount(new BigDecimal(10));
        return order;
    }
}

